package com.proton.carepatchtemp.database.daomanager;

import android.database.sqlite.SQLiteDatabase;

import com.apkfuns.logutils.LogUtils;
import com.proton.carepatchtemp.application.VCare;
import com.proton.carepatchtemp.constant.Settings;
import com.proton.carepatchtemp.database.gen.DaoMaster;
import com.proton.carepatchtemp.database.gen.DaoSession;

/**
 * Created by MoonlightSW on 2017/2/15.
 */

public class GreenDaoManager {
    private DaoMaster mDaoMaster;
    private DaoSession mDaoSession;
    private DaoMaster.DevOpenHelper mDevOpenHelper;
    private SQLiteDatabase mDatabase;

    private GreenDaoManager() {
        init();
    }

    /**
     * 对外唯一实例的接口
     */
    public static GreenDaoManager get() {
        return SingleInstanceHolder.INSTANCE;
    }

    /**
     * 初始化数据
     */
    public void init() {
        mDevOpenHelper = new DaoMaster.DevOpenHelper(VCare.get(),
                Settings.DB_NAME);
        mDatabase = mDevOpenHelper.getWritableDatabase();
        mDaoMaster = new DaoMaster(mDatabase);
        mDaoSession = mDaoMaster.newSession();
        LogUtils.d("Init GreenDao DB!");
    }

    public DaoMaster getmDaoMaster() {
        return mDaoMaster;
    }

    public DaoSession getmDaoSession() {
        return mDaoSession;
    }

    public DaoSession getNewSession() {
        mDaoSession = mDaoMaster.newSession();
        return mDaoSession;
    }

    public SQLiteDatabase getDatabase() {
        return mDatabase;
    }

    public DaoMaster.DevOpenHelper getDevOpenHelper() {
        return mDevOpenHelper;
    }

    /**
     * 静态内部类，实例化对象使用
     */
    private static class SingleInstanceHolder {
        private static final GreenDaoManager INSTANCE = new GreenDaoManager();
    }

}
